<?php

/**
 *  限时特购程序
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
assign_template();
$_REQUEST['act'] = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : '';
$smarty->assign('top_title', $_LANG['limit']);
$smarty->assign('footer', get_footer());
// $smarty->assign('limit', get_promote_goods());


    $now_time  = time();//当前时间
    $test_time = time();//测试时间
    // $test_time = "1415635210";//测试时间
    //根据当前时间判断属于第几场
    if($test_time<='1415635200'){
        $buy_num=1;
    }
    elseif ($test_time>'1415635200') {
        $buy_num=2;
    }
    elseif ($test_time>'1415664000') {
        $buy_num=3;
    }
    elseif ($test_time>'1415682000') {
        $buy_num=4;
    }
    elseif ($test_time>'1415707200') {
        $buy_num=5;
    }
    // 判断是否点击了场数
    $now_num=!empty($_REQUEST['buy_num'])?$_REQUEST['buy_num']:$buy_num;
    switch ($now_num) {
        case '1':
            $time='1415628000';
            break;
        case '2':
            $time='1415635200';
            break;
        case '3':
            $time='1415664000';
            break;
        case '4':
            $time='1415682000';
            break;
        case '5':
            $time='1415707200';
            break;
        
        default:
            $time=time();
            break;
    }
    //如果当前点击场数与当前进行场数相同则显示最后疯抢
    if(($_REQUEST['buy_num'] == $buy_num)||empty($_REQUEST['buy_num'])){
        //最后疯抢 
        $sql = 'SELECT g.goods_id, g.goods_name, g.promote_start_date,g.promote_end_date, g.goods_thumb , g.promote_price,g.market_price,sum(gs.goods_number) AS goods_stock ' .
        ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
        ' LEFT JOIN '.$GLOBALS['ecs']->table('goods_stock') . ' AS gs ON gs.goods_id = g.goods_id' .
        ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
        " AND g.promote_start_date >= '1415628000' AND g.promote_start_date < '$test_time' AND g.promote_start_date != '$time'".
        " GROUP BY g.goods_id ";
        $last_goods=$GLOBALS['db']->getAll($sql);        
        foreach ($last_goods AS $idx => $row)
        {   
            $last_goods[$idx]['end'] = ($row['promote_end_date']-$test_time)<0?1:0;
            $last_goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
        }
    }


    //第一场都没有开始

        //第一场商品
        $sql = 'SELECT g.goods_id, g.goods_name, g.promote_start_date,g.promote_end_date, g.goods_thumb , g.promote_price,g.market_price ,sum(gs.goods_number) AS goods_stock ' .
        ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
        ' LEFT JOIN '.$GLOBALS['ecs']->table('goods_stock') . ' AS gs ON gs.goods_id = g.goods_id' .
        ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
        " AND g.promote_start_date = '$time'".
        " GROUP BY g.goods_id ";
        $first_goods = $GLOBALS['db']->getAll($sql);
        foreach ($first_goods AS $idx => $row)
        {   
            $first_goods[$idx]['end'] = ($row['promote_end_date']-$test_time)<0?1:0;
            $first_goods[$idx]['promote_date'] = time_start(($row['promote_start_date']-$test_time)) ;
            $first_goods[$idx]['lave_time'] =intval($row['promote_start_date']-$test_time);
            $first_goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
        }
    $open_time=1415628000-time();
    $smarty->assign('open_time_t', time_start($open_time));
    $smarty->assign('open_time', $open_time); 

    foreach ($first_goods as $key => $value) {
        if(time() > $time){
            $sql = "SELECT SUM(goods_number) FROM " . $ecs->table('cart') . " WHERE goods_id = $value[goods_id]";
            $s1 = $db->getOne($sql) * $value['sku'];
            $sql = "SELECT SUM(goods_number) FROM " . $ecs->table('order_goods') . " WHERE goods_id = $value[goods_id] AND add_time >$time";
            $s2 = $db->getOne($sql)*$value['sku'];
        }
        $first_goods[$key]['sumbuy'] = $s1 + $s2;
    }


    
    $smarty->assign('first_goods', $first_goods); 
    $smarty->assign('last_goods', $last_goods);  
    $smarty->assign('buy_num', $now_num);

$smarty->display('limit.dwt');

/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */
/**
 *
 *
 * @access public
 * @param
 *
 * @return void
 */
function is_not_null($value)
{
    if (is_array($value))
    {
        return (!empty($value['from'])) || (!empty($value['to']));
    }
    else
    {
        return !empty($value);
    }
}
/*格式化时间戳函数*/
function  time_start($data){
        $Y = '';
        $D = '';
        $H = '';
        $M = '';
        $T = '';
        if(intval($data/3600)!=0){
          $H=intval($data/3600)."时";
        }else if($D!=''){
          $H='0'."时";
        }
        if(intval($data%(3600)/60)!=0){
          $M=intval((($data%(3600))/60))."分";
        }else if($H!=''){
          $M='0'."分";
        }
        if(intval($data%60)!=0){
          $T=intval($data%60)."秒";
        }else if($M!=''){
          $T='0'."秒";
        }
        return $Y.$D.$H.$M.$T;
      }
/**
 * 获得可以检索的属性
 *
 * @access  public
 * @params  integer $cat_id
 * @return  void
 */
function get_seachable_attributes($cat_id = 0)
{
    $attributes = array(
        'cate' => array(),
        'attr' => array()
    );

    /* 获得可用的商品类型 */
    $sql = "SELECT t.cat_id, cat_name FROM " .$GLOBALS['ecs']->table('goods_type'). " AS t, ".
           $GLOBALS['ecs']->table('attribute') ." AS a".
           " WHERE t.cat_id = a.cat_id AND t.enabled = 1 AND a.attr_index > 0 ";
    $cat = $GLOBALS['db']->getAll($sql);

    /* 获取可以检索的属性 */
    if (!empty($cat))
    {
        foreach ($cat AS $val)
        {
            $attributes['cate'][$val['cat_id']] = $val['cat_name'];
        }
        $where = $cat_id > 0 ? ' AND a.cat_id = ' . $cat_id : " AND a.cat_id = " . $cat[0]['cat_id'];

        $sql = 'SELECT attr_id, attr_name, attr_input_type, attr_type, attr_values, attr_index, sort_order ' .
               ' FROM ' . $GLOBALS['ecs']->table('attribute') . ' AS a ' .
               ' WHERE a.attr_index > 0 ' .$where.
               ' ORDER BY cat_id, sort_order ASC';
        $res = $GLOBALS['db']->query($sql);

        while ($row = $GLOBALS['db']->FetchRow($res))
        {
            if ($row['attr_index'] == 1 && $row['attr_input_type'] == 1)
            {
                $row['attr_values'] = str_replace("\r", '', $row['attr_values']);
                $options = explode("\n", $row['attr_values']);

                $attr_value = array();
                foreach ($options AS $opt)
                {
                    $attr_value[$opt] = $opt;
                }
                $attributes['attr'][] = array(
                    'id'      => $row['attr_id'],
                    'attr'    => $row['attr_name'],
                    'options' => $attr_value,
                    'type'    => 3
                );
            }
            else
            {
                $attributes['attr'][] = array(
                    'id'   => $row['attr_id'],
                    'attr' => $row['attr_name'],
                    'type' => $row['attr_index']
                );
            }
        }
    }

    return $attributes;
}
?>